package com.gmlive.common.sample.widgets

import android.content.Context
import android.media.MediaPlayer
import android.os.Bundle
import android.util.Log
import com.gmlive.common.sample.R
import com.gmlive.common.ui.app.IkCompatActivity
import com.gmlive.common.ui.app.annotation.Immerse
import kotlinx.android.synthetic.main.activity_video_gift_player.*
import java.io.*
import java.util.concurrent.atomic.AtomicInteger

@Immerse(translucentStatus = true)
class VideoGiftPlayerActivity: IkCompatActivity() {

    private val resArray = arrayListOf("love_gift.mp4", "jisutianmi.mp4")
    private val nextIndex = AtomicInteger(0)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_video_gift_player)

        copyVideo()

        video_surface_view.setOnCompletionListener(MediaPlayer.OnCompletionListener {
            //playNextVideo()
        })

        play_button.setOnClickListener {
            playNextVideo()
        }
    }

    private fun playNextVideo() {
        val res = resArray[nextIndex.getAndIncrement() % 2]
        video_surface_view.setVideoPath(cacheDir.absolutePath + "/$res")
        video_surface_view.start()
    }

    override fun onStart() {
        super.onStart()
//        video_surface_view.start()
    }

    override fun onResume() {
        super.onResume()
        video_surface_view.onResume()
//        video_surface_view.resume()
    }

    override fun onPause() {
        super.onPause()
//        video_surface_view.pause()
        video_surface_view.onPause()
    }

    override fun onStop() {
        super.onStop()

        video_surface_view.stopPlayback()
    }

    override fun onDestroy() {
        super.onDestroy()
        video_surface_view.stopPlayback()
    }

    private fun copyVideo() {
        Thread(Runnable {
            doCopy(this, "", cacheDir.absolutePath)
            runOnUiThread {
                video_surface_view.setVideoPath(cacheDir.absolutePath + "/jisutianmi.mp4")
//                video_surface_view.start()
            }
        }).start()
    }

    companion object {
        @Throws(IOException::class)
        fun doCopy(context: Context, assetsPath: String, desPath: String) {
            context.assets.list(assetsPath)?.forEach {
                val outFileName = desPath + File.separator.toString() + it
                var inFileName = assetsPath + File.separator.toString() + it
                if (assetsPath == "") { // for first time
                    inFileName = it
                }
                Log.e("tag", "========= assets: $assetsPath  filename: $it infile: $inFileName outFile: $outFileName")
                try {
                    val inputStream: InputStream = context.assets.open(inFileName)
                    val destFile = File(outFileName)
                    if (destFile.exists() && destFile.isFile) {
                        return@forEach
                    }
                    copyAndClose(inputStream, FileOutputStream(outFileName))
                } catch (e: IOException) { //if directory fails exception
                    e.printStackTrace()
                    File(outFileName).mkdir()
                    doCopy(context, inFileName, outFileName)
                }
            }
        }

        private fun closeQuietly(out: OutputStream?) {
            try {
                out?.close()
            } catch (ex: IOException) {
                ex.printStackTrace()
            }
        }

        private fun closeQuietly(`is`: InputStream?) {
            try {
                `is`?.close()
            } catch (ex: IOException) {
                ex.printStackTrace()
            }
        }

        @Throws(IOException::class)
        private fun copyAndClose(`is`: InputStream, out: OutputStream) {
            copy(`is`, out)
            closeQuietly(`is`)
            closeQuietly(out)
        }

        @Throws(IOException::class)
        private fun copy(`is`: InputStream, out: OutputStream) {
            val buffer = ByteArray(1024)
            var n = 0
            while (-1 != `is`.read(buffer).also { n = it }) {
                out.write(buffer, 0, n)
            }
        }
    }
}